﻿using System;
using Eve.TapToClick.Core.Control;
using Eve.TapToClick.Models;

namespace Eve.TapToClick.Core.Control
{
    class Control1 : MainControl
    {
        // 按下按钮时长
        private DateTime[] DownClickDateTime = new DateTime[2];
        // 松开按钮时长
        private DateTime[] UpClickDateTime = new DateTime[2];
        // 连续点击次数
        private uint ContinuousClick = 0;

        private void addDownClickDateTime(DateTime dt)
        {
            if (DownClickDateTime[0] != null)
            {
                DownClickDateTime[0] = DownClickDateTime[1];
                DownClickDateTime[1] = dt;
            }
            else
            {
                DownClickDateTime[0] = dt;
            }
        }

        private void addUpClickDateTime(DateTime dt)
        {
            if(UpClickDateTime[0] != null)
            {
                UpClickDateTime[0] = UpClickDateTime[1];
                UpClickDateTime[1] = dt;
            }
            else
            {
                UpClickDateTime[0] = dt;
            }
        }

        public Control1(AppConfiguration appConfiguration, MouseButtonCase mouseButtonCase) : base(appConfiguration, mouseButtonCase, 1)
        {
        }

        public override void First()
        {
            addDownClickDateTime(DownDateTime);
            if (ContinuousClick > 0 && (DownClickDateTime[1] - DownClickDateTime[0]).TotalMilliseconds < appConfiguration.ContinuousClickTime) {
                mouseButtonCase.DownLeft();
            }
        }

        public override void Last()
        {
            if (SingleClick())
            {
                addUpClickDateTime(UpDateTime);
                // 记录连点信息
                if (ContinuousClick > 0)
                {
                    // 判断时间是否合法
                    if((UpClickDateTime[1] - UpClickDateTime[0]).TotalMilliseconds < appConfiguration.ContinuousClickTime)
                    {
                        // 连点数加一
                        if (ContinuousClick > 0)
                        {
                            mouseButtonCase.UpLeft();
                        }
                        ContinuousClick++;
                    }
                    else
                    {
                        // 重置连点数
                        if (ContinuousClick > 0)
                        {
                            mouseButtonCase.UpLeft();
                        }
                        ContinuousClick = 0;
                    }
                }
                else
                {
                    // 第一次点击
                    ContinuousClick++;
                    mouseButtonCase.SendLeftClick();
                }
            } else
            {
                // 重置连点数
                if (ContinuousClick > 0) {
                    mouseButtonCase.UpLeft();
                }
                ContinuousClick = 0;
            }
        }

        public override void Sustained()
        {
            uint x1 = XValues[0], y1 = YValues[0];
            uint x2 = XValues[1], y2 = YValues[1];

            int XDiff = 0, YDiff = 0;

            if(x1 > x2)
            {
                // x轴向左
                XDiff = -(int)(x1 - x2);
            } else if(x1 < x2) {
                // x轴向右
                XDiff = (int)(x2 - x1);
            }

            if (y1 > y2)
            {
                // y轴向上
                YDiff = -(int)(y1 - y2);
            }
            else if (y1 < y2)
            {
                // y轴向下
                YDiff = (int)(y2 - y1);
            }

            XDiff = (int)(XDiff * appConfiguration.MoveSpeed);
            YDiff = (int)(YDiff * appConfiguration.MoveSpeed);

            if ((XDiff != 0 || YDiff != 0) && !SingleClick()) {
                mouseButtonCase.SendMouseMove(XDiff, YDiff);
            }
        }
    }
}
